﻿using System;
using System.IO;
using Microsoft.Xna.Framework;

namespace Babylon
{
    public static class Tools
    {
        public static Vector2 ReadVector2(this BinaryReader reader)
        {
            return new Vector2(reader.ReadSingle(), reader.ReadSingle());
        }

        public static Vector3 ReadVector3(this BinaryReader reader)
        {
            return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
        }

        public static Matrix ReadMatrix(this BinaryReader reader)
        {
            return new Matrix
                (
                    reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                    reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                    reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                    reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()
                );
        }

        public static Color ReadColor(this BinaryReader reader)
        {
            return new Color(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
        }

        public static Guid ReadGuid(this BinaryReader reader)
        {
            return Guid.Parse(reader.ReadString());
        }

        public static Color Multiply(Color c1, Color c2)
        {
            float r = (c1.R / 255.0f) * (c2.R / 255.0f);
            float g = (c1.G / 255.0f) * (c2.G / 255.0f);
            float b = (c1.B / 255.0f) * (c2.B / 255.0f);
            float a = (c1.A / 255.0f) * (c2.A / 255.0f);

            return new Color(r, g, b, a);
        }

        public static float DotCoordinate(this Plane plane, Vector3 point)
        {
            return ((((plane.Normal.X * point.X) + (plane.Normal.Y * point.Y)) + (plane.Normal.Z * point.Z)) + plane.D);
        }


        public static Vector4[] ToVector4Array(this Matrix matrix)
        {
            Vector4[] result = new Vector4[4];

            result[0] = new Vector4(matrix.M11, matrix.M12, matrix.M13, matrix.M14);
            result[1] = new Vector4(matrix.M21, matrix.M22, matrix.M23, matrix.M24);
            result[2] = new Vector4(matrix.M31, matrix.M32, matrix.M33, matrix.M34);
            result[3] = new Vector4(matrix.M41, matrix.M42, matrix.M43, matrix.M44);

            return result;
        }
    }
}